/**
 * @Author yinyunqi
 * @date 2017年12月28日
 * @Content 预算
 */
package com.yongqi.controller.console;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.yongqi.model.Family;
import com.yongqi.model.console.Budget;
import com.yongqi.service.MemberService;
import com.yongqi.service.console.BudgetService;

@Controller
@RequestMapping("/budget")
public class BudgetController {
	@Autowired
	BudgetService budgetService;
	@Autowired
	MemberService memberService;

	/**
	 * 跳转至预算主页面 根据本家庭账户获得该家庭账户下拥有的成员账户,赋值于预算制定人
	 * 
	 * @return
	 */
	@RequestMapping("/index")
	public String chargeIndex(Model model) {
	    	String familyAccount = ((Family) SecurityUtils.getSubject().getPrincipal()).getFamilyaccount();
		List<String> memberNametList = memberService.selectMemberByFamily(familyAccount);
		model.addAttribute("nameList", memberNametList);
		return "budget/BudgetIndex";
	}

	/**
	 * 添加预算记录
	 * 
	 * @param request
	 * @return
	 * @throws SQLException
	 */
	@ResponseBody
	@RequestMapping("/addBudget")
	public String addBudget(HttpServletRequest request) throws SQLException {
		Budget budget = new Budget();
		String familyAccount = ((Family) SecurityUtils.getSubject().getPrincipal()).getFamilyaccount();
		budget.setId(UUID.randomUUID().toString());
		budget.setFamilyaccount(familyAccount);
		budget.setStartdate(request.getParameter("budgetStartDate"));
		budget.setEnddate(request.getParameter("budgetEndDate"));
		budget.setItem(request.getParameter("budgetItem"));
		budget.setTip(request.getParameter("budgetTip"));
		budget.setType(request.getParameter("budgetType"));
		budget.setMember(request.getParameter("budgetMember"));
		budget.setMoney(request.getParameter("budgetMoney"));
		budgetService.budgetAdd(budget);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("result", "success");
		return jsonObject.toJSONString();
	}
	/**
     * 预算分页查询
     * @param request
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping("/query")
    public String queryItems(HttpServletRequest request,@RequestParam int pageNumber ,int pageSize) {
	String familyAccount = ((Family) SecurityUtils.getSubject().getPrincipal()).getFamilyaccount();
	Map<String, String> map = new HashMap<String, String>();
	map.put("familyAccount", familyAccount);
		int total = budgetService.queryPageItems(map).size();
		PageHelper.startPage(pageNumber,pageSize);
		List<Budget> incomeList = budgetService.queryPageItems(map);
		JSONObject object = new JSONObject();
		object.put("rows", incomeList);
		object.put("total", total);
		return object.toJSONString();
    }
    /**
     * 删除预算记录
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete")
    public String deleteItems(HttpServletRequest request) {
		String[] ids = request.getParameterValues("ids");
		for(int i=0;i<ids.length;i++) {
			budgetService.deleteIncomeItems(ids[i]);
		}
		JSONObject object = new JSONObject();
		object.put("result", "success");
		return object.toJSONString();
    }
    /**
     * 修改预算记录
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateItem")
    public String updateItem(HttpServletRequest request) {
		Budget budget = new Budget();
		String familyAccount = ((Family) SecurityUtils.getSubject().getPrincipal()).getFamilyaccount();
		budget.setId(request.getParameter("id"));
		budget.setFamilyaccount(familyAccount);
		budget.setStartdate(request.getParameter("budgetStartDate"));
		budget.setEnddate(request.getParameter("budgetEndDate"));
		budget.setItem(request.getParameter("budgetItem"));
		budget.setTip(request.getParameter("budgetTip"));
		budget.setType(request.getParameter("budgetType"));
		budget.setMember(request.getParameter("budgetMember"));
		budget.setMoney(request.getParameter("budgetMoney"));
		budgetService.incomeUpdate(budget);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("result", "success");
		return jsonObject.toJSONString();
    }
}
